//
//  EKCellModelsManager.m
//  Eakay
//
//  Created by zach on 2018/12/13.
//  Copyright © 2018年 恒天易开软件科技股份有限公司. All rights reserved.
//

#import "EKTableDataSource.h"

@interface EKTableDataSource ()
@property (nonatomic, strong) NSMutableDictionary *sections;

@end

@implementation EKTableDataSource

- (instancetype)init {
    self = [super init];
    _sections = [[NSMutableDictionary alloc] init];
    return self;
}

- (void)addItems:(NSArray <id<EKCellModelProtocol>>*)items; {
    [self.sections setObject:items forKey:@0];
}

- (void)addItems:(NSArray <id<EKCellModelProtocol>>*)items atSection:(NSInteger)section; {
    [self.sections setObject:items forKey:@(section)];
}

- (void)addItem:(id<EKCellModelProtocol>)item atIndexPath:(NSIndexPath *)indexPath; {
    NSArray *rows = [self.sections objectForKey:@(indexPath.section)];
    NSMutableArray *mRows = rows.mutableCopy;
    NSAssert(indexPath.row > 0 && indexPath.row < mRows.count+1, @"Wrong indexPath.row, out of insert range..");
    if (indexPath.row < mRows.count) {
        [mRows insertObject:item atIndex:indexPath.row];
    }
    else {
        [mRows addObject:item];
    }
    [self.sections setObject:mRows forKey:@(indexPath.section)];
}

- (void)addItem:(id<EKCellModelProtocol>)item atSection:(NSInteger)section; {
    NSArray *rows = [self.sections objectForKey:@(section)];
    NSMutableArray *mRows = rows.mutableCopy;
    [mRows addObject:item];
    [self.sections setObject:mRows forKey:@(section)];
}

- (id<EKCellModelProtocol>)itemForIndexPath:(NSIndexPath *)indexPath; {
    if ([self.sections objectForKey:@(indexPath.section)]) {
        return [self.sections objectForKey:@(indexPath.section)][indexPath.row];
    }
    return nil;
}

- (NSIndexPath *)indexPathForItem:(id<EKCellModelProtocol>)item; {
    
    __block NSUInteger row = 0;
    __block NSUInteger section = 0;
    
    [self.sections enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, NSArray <id<EKCellModelProtocol>>*rows, BOOL * _Nonnull stop1) {
        
        [rows enumerateObjectsUsingBlock:^(id<EKCellModelProtocol>  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop2) {
            if (obj == item) {
                row = idx;
                section = [key unsignedIntegerValue];
                *stop1 = YES;
                *stop2 = YES;
            }
        }];
    }];
    return [NSIndexPath indexPathForRow:row inSection:section];
}

- (void)configureCellBlock:(ConfigureCellBlock)configureCell; {
    _configureCellBlock = configureCell;
}

#pragma mark - UITableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.allKeys.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *rows = [self.sections objectForKey:@(section)];
    return rows.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    
    id<EKCellModelProtocol> item = [self itemForIndexPath:indexPath];
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:item.reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:item.reuseIdentifier];
    }
    item.configContentViewBlock ? item.configContentViewBlock(item) : nil;
    
    // keep it, maybe I will make a change :)
    _configureCellBlock(cell, item);
    return cell;
}


@end
